package com.ly.system.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.dto.CommonBatchUpdateDTO;
import com.ly.system.system.bean.entity.SysDictItem;
import com.ly.system.service.ISysDictItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 字典明细
 *
 * @author jeecg
 * @since 2018-12-28
 */
@Tag(name = "数据字典")
@Slf4j
@RestController
@RequestMapping("/sys/dictItem")
@RequiredArgsConstructor
public class SysDictItemController {

    private final ISysDictItemService sysDictItemService;

    /**
     * 查询-id
     *
     * @param id
     * @return
     */
    @Operation(summary = "查询-id")
    @GetMapping("/getId")
    public R<SysDictItem> getId(@RequestParam(name = "id") String id) {
        return sysDictItemService.getId(id);
    }

    /**
     * 列表
     *
     * @param entity
     * @return
     */
    @Operation(summary = "列表")
    @GetMapping("/getList")
    public R<List<SysDictItem>> getList(SysDictItem entity) {
        return sysDictItemService.getList(entity);
    }

    /**
     * 分页
     *
     * @param entity
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Operation(summary = "分页")
    @GetMapping("/getPage")
    public R<IPage<SysDictItem>> getPage(
            SysDictItem entity,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return sysDictItemService.getPage(entity, new Page<SysDictItem>(pageNo, pageSize));
    }

    /**
     * 新增
     *
     * @param entity
     * @return
     */
    @Operation(summary = "新增")
    @PostMapping("/add")
    public R<String> add(@RequestBody SysDictItem entity) {
        return sysDictItemService.add(entity);
    }

    /**
     * 修改
     *
     * @param entity
     * @return
     */
    @Operation(summary = "修改")
    @PostMapping("/edit")
    public R<String> edit(@RequestBody SysDictItem entity) {
        return sysDictItemService.edit(entity);
    }

    /**
     * 删除-id
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除-id")
    @DeleteMapping("/delete")
    public R<String> delete(@RequestParam(name = "id") String id) {
        return sysDictItemService.delete(id);
    }

    /**
     * 批量新增或修改
     *
     * @param dto
     * @return
     */
    @Operation(summary = "批量新增或修改")
    @PostMapping("/updateBatch")
    public R updateBatch(@Validated @RequestBody CommonBatchUpdateDTO<SysDictItem> dto) {
        return sysDictItemService.updateBatch(dto);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Operation(summary = "批量删除")
    @DeleteMapping("/deleteBatch")
    public R<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        return sysDictItemService.deleteBatch(ids);
    }

    /**
     * 字典值重复校验
     *
     * @param sysDictItem
     * @param request
     * @return
     */
    @GetMapping("/dictItemCheck")
    @Operation(summary = "字典重复校验接口")
    public R<String> doDictItemCheck(SysDictItem sysDictItem, HttpServletRequest request) {
        Long num = sysDictItemService.count(
                new LambdaQueryWrapper<SysDictItem>()
                        .eq(SysDictItem::getItemValue, sysDictItem.getItemValue())
                        .eq(SysDictItem::getDictId, sysDictItem.getDictId())
                        .ne(StrUtil.isNotBlank(sysDictItem.getId()), SysDictItem::getId, sysDictItem.getId())
        );
        if (num != 0) {
            // 该值可用
            return R.fail("该值不可用，系统中已存在！");
        }
        return R.ok("该值可用");
    }
}
